from comment.comviews import *
from utils.dbutils import DB_Config, MysqlTools
from utils.formatutils import get_params
from utils.pageutils import paging_simple
from utils.tokens import *
from .sqlmap import *
from django.http import HttpResponse
import json
from user_agents import parse

mysql_tools = MysqlTools(db_config=DB_Config.get("default"))


def add_note(request):
    """
    添加密码本
    :param request:
    :return:
    """
    params = {
        "target": "target",
        "data_username": "username",
        "data_password": "password",
        "remarks": "remarks",
        "user_id": "uid"
    }
    body = get_params(request.body, params)
    if isinstance(body, JsonResponse):
        return body
    note_sql = NoteSqlMap.ADD_NOTE.format(**body)
    note_id = mysql_tools.execute_sql(note_sql)
    if note_id is None:
        return err_response(110)
    
    body["note_id"] = note_id
    
    record_id = str(uuid.uuid4()).replace("-", "")
    body["record_id"] = record_id
    
    record_sql = RecordSqlMap.ADD_RECORD.format(**body)
    result = mysql_tools.execute_sql(record_sql, errcode=110)
    if isinstance(result, JsonResponse):
        mysql_tools.execute_sql(NoteSqlMap.DEL_NOTE.format(**body))
        return result
    return success_response(result)


def note_info(request):
    """
    密码本信息
    :param request:
    :return:
    """
    params = {
        "note_id": "note_id"
    }
    body = get_params(request.body, params)
    if isinstance(body, JsonResponse):
        return body
    note_sql = NoteSqlMap.NOTE_INFO.format(**body)
    result = mysql_tools.select_one(note_sql)
    if isinstance(result, JsonResponse):
        return result
    return success_response(result)


def del_note(request):
    """
    删除密码本
    :param request:
    :return:
    """
    params = {
        "target": "target",
        "delete_time": "delete_time"
    }
    body = get_params(request.body, params)
    if isinstance(body, JsonResponse):
        return body
    sql = RecordSqlMap.DEL_NOTE_UPD_RECORD.format(**body)
    result = mysql_tools.execute_sql(sql, errcode=120)
    if isinstance(result, JsonResponse):
        return result
    return success_response(result)


def save_note(request):
    """
    保存密码本
    :param request:
    :return:
    """
    params = {
        "target": "target",
        "data_username": "username",
        "data_password": "password",
        "remarks": "remarks",
        "user_id": "uid",
        "note_id": "note_id",
        "update_time": "update_time"
    }
    body = get_params(request.body, params)
    if isinstance(body, JsonResponse):
        return body
    note_sql = NoteSqlMap.UPD_NOTE.format(**body)
    note_result = mysql_tools.execute_sql(note_sql)
    if isinstance(note_result, JsonResponse):
        return note_result
    
    record_sql = RecordSqlMap.UPD_NOTE_UPD_RECORD.format(**body)
    result = mysql_tools.execute_sql(record_sql)
    if isinstance(result, JsonResponse):
        mysql_tools.execute_sql(NoteSqlMap.DEL_NOTE.format(**body))
        return result
    return success_response(result)


def note_list(request):
    """
    密码本列表
    :param request:
    :return:
    """
    params = {
        "user_id": "uid"
    }
    null_parmas = {
        "size": "size",
        "page": "page",
        "isall": "isAll"
    }
    body = get_params(request.body, params, null_parmas)
    if isinstance(body, JsonResponse):
        return body
    sql = NoteSqlMap.NOTE_LIST.format(**body)
    result = mysql_tools.execute_sql(sql)
    if isinstance(result, JsonResponse):
        return result
    if body.get("isall") is False:
        result = paging_simple(body, body.get("page"), body.get("size"))
    return success_response(result)


def sync_note(request):
    """
    密码本同步
    :param request:
    :return:
    """
    params = {
        "user_id": "uid",
        "sync_time": "now"
    }
    body = get_params(request.body, params)
    if isinstance(body, JsonResponse):
        return body
    note_sql = NoteSqlMap.NOTE_SYNC.format(**body)
    result = mysql_tools.select_all(note_sql)
    if isinstance(result, JsonResponse):
        return result
    return success_response(result)


@format_response
def index(request):
    return com_views(request, {
        "addnote": add_note,
        "noteinfo": note_info,
        "delnote": del_note,
        "notelist": note_list,
        "syncnote": sync_note,
    })
